app.controller('UploadCtrl',function($scope,$localStorage){

        $('#file_upload').uploadify({
            'swf'      : 'js/uploadify/uploadify.swf',   
            'uploader' : "http://118.178.136.209/index.php?r=admin-user/upload-head-img",
            'buttonText': '上传头像',
            'fileObjName' : 'picture',
            'fileTypeDesc' : '选择文件',
            'fileTypeExts' : '*.jpg;*.png;',
            'method' : 'post',
            'formData':{'token':$localStorage.token},
            'fileSizeLimit':'10MB',
            'auto' : true,
            'multi' : false,
            'onUploadSuccess' : function(file,data,response) {
              
              var filedata = JSON.parse(data);
              if(filedata.code==0&&filedata.msg=="上传成功") {
                $("#head_img").val(filedata.data);
                $("#file_uploaded11").text(filedata.data+"上传完成");
              }
              
              
              
            }
        });
   
})
app.controller('NewUserModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var name, username,password,repeat_pwd,genter,phone,mail,apartment,post,head_img;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    name = $scope.oo.name;
                    username = $scope.oo.username; 
                    password = $scope.oo.password;  
                    repeat_pwd = $scope.oo.repeat_pwd;
                    genter = $scope.oo.genter;
                    phone = $scope.oo.phone;
                    mail = $scope.oo.mail;
                    apartment = $scope.oo.apartment; 
                    post = $scope.oo.post;
                    head_img = $("#head_img").val();

                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newuserModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {

                    $http.post(
                        $scope.app.backendurl+"admin-user/create-user",
                        {
                            'name': name,
                            'username': username,
                            'password':password,
                            'repeat_pwd':repeat_pwd,
                            'genter':genter,
                            'phone':phone,
                            'mail':mail ,
                            'apartment':apartment, 
                            'post':post,
                            'head_img':"http://118.178.136.209/"+head_img,                  
                            'token':$localStorage.token             

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="创建用户账号成功"){
                        alert(data.msg);
                        //$window.location.reload('app/sys/user');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                     if(data.code==1&&(data.msg=="已经存相同用户名的帐号"||data.msg=="Mail不是有效的邮箱地址。")){
                        alert(data.msg+"请重新创建");

                      }
                      if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])

app.controller('EditUserModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var name, username,genter,phone,mail,apartment,post,head_img,version;
            var ud_name,ud_username,ud_genter,ud_phone,ud_mail,ud_apartment,ud_post,ud_head_img,ud_version,user_id;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.name = name;
                $scope.oo.username = username; 
                $scope.oo.genter = genter;
                $scope.oo.phone = phone;
                $scope.oo.mail = mail;
                $scope.oo.apartment = apartment;
                $scope.oo.post = post;
                $("#head_img").val(head_img);
                $scope.oo.version = version;


                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    ud_name = $scope.oo.name;
                    ud_username = $scope.oo.username;  
                    ud_genter =  $scope.oo.genter;
                    ud_phone = $scope.oo.phone;
                    ud_mail = $scope.oo.mail;
                    ud_apartment = $scope.oo.apartment;
                    ud_post = $scope.oo.post;
                    ud_head_img = "http://118.178.136.209/"+$("#head_img").val();
                    ud_version = $scope.oo.version;

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };

            $scope.edituser = function(obj){
                //console.log(obj);
                name = obj.name;
                user_id = obj.user_id;
                username = obj.uname;   
                genter = obj.genter;
                phone = obj.phone;
                mail = obj.mail;
                apartment = obj.apartment;
                post = obj.post;
                head_img = obj.head_img;
                version = obj.version;            
                $scope.open();

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'edituserModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"admin-user/update-user",
                        {
                            'user_id': user_id,
                            'name': ud_name,
                            'username': ud_username,                           
                            'genter':ud_genter,
                            'phone':ud_phone,
                            'mail':ud_mail ,
                            'apartment':ud_apartment, 
                            'post':ud_post,
                            'head_img':ud_head_img,  
                            'version':ud_version,               
                            'token':$localStorage.token                            

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="更新用户资料成功"){
                        alert(data.msg);
                        //$window.location.reload('app/sys/user');

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                    

                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])


app.controller('DelUserModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var username;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.deluser = function (aid) {
                //console.log(aid);
                username = aid;             
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'deluserModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"admin-user/delete-user",
                        {
                           
                            'username': username,                             
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      console.log(data);
                      if(data.code==0&&data.msg=="删除用户账号成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/user');

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })


                });
            };
        }])